<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once '/application/libraries/JssdkLogic.php';
class Agentapi extends CI_Controller {

	public function __construct(){
		parent::__construct();        
        ini_set('date.timezone','Asia/Shanghai');
        $this->load->model('Model_m');// 第三方 实例 增删改查 的模型
        $this->load->model('Config_m');
        $this->load->model('Commission_m');
        $this->load->model('Settlementlog_m');
        $this->load->model('Device_m');
        $this->load->model('Order_m');
        $this->load->model('Users_m');
        $this->load->model('Agent_m');
        $this->load->model('Recharge_m');
        $this->load->library('session');
        $this->load->library('api/agentapi_service');
		//header('Content-type:application/x-www-form-urlencoded');
	}

	public function agent_api()
	{	
        switch ($_SERVER["REQUEST_METHOD"]) {
            case 'GET':
                /**
                 * 代理登录
                 * @param agent_qq  账号
                 * @param password  密码
                 */
                $where['agent_qq'] = $this->input->get('agent_qq');
                $where['password'] = $this->input->get('password');
                if(empty($where['agent_qq'])||empty($where['password'])){
                    $data['success'] = error('4008'); //请求要求成功返回信息
                    echo _echo($data);
                    exit;
                }
                $result=$this->agentapi_service->getAgentByqqAndPassword($where);
                echo $result;
                break;
            case 'POST':
                /**
                 * 添加二级代理
                 * @param tooken 一级代理临时通行证
                 * @param agent_qq      账号
                 * @param agent_name    名字
                 * @param sex           性别
                 * @param mobile        手机号
                 * @param idcard        身份证号
                 * @param bankcard      银行卡号
                 * @param bankname      银行名称
                 */
                $tooken = $this->input->post('tooken');
                if(empty($tooken)){
                    $data['success'] = error('4008');
                    echo _echo($data);
                    exit;
                }
                $agentid=jiemi($tooken);
                $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));

                if(empty($agent)){
                    $data['success'] = error('4011');
                    echo _echo($data);
                    exit;
                }                
                $agent_qq = $this->input->post('agent_qq');
                if($agent_qq){
                    $post['agent_qq'] =$agent_qq;
                }
                $agent_name = $this->input->post('agent_name');
                if($agent_name){
                    $post['agent_name'] =$agent_name;
                }
                $mobile = $this->input->post('mobile');
                if($mobile){
                    $post['mobile'] =$mobile;
                }
                $idcard = $this->input->post('idcard');
                if($idcard){
                    $post['idcard'] =$idcard;
                }
                $sex = $this->input->post('sex');
                if($sex){
                    $post['sex'] =1;
                }else{
                	$post['sex'] =0;
                }
                $bankcard = $this->input->post('bankcard');
                if($bankcard){
                    $post['bankcard'] =$bankcard;
                }
                $bankname = $this->input->post('bankname');
                if($bankname){
                    $post['bankname'] =$bankname;
                }
                $branch = $this->input->post('branch');
                if($branch){
                    $post['branch'] =$branch;
                }
                $post['addtime'] = date('YmdHis');
                $post['agent_level'] = 2;
                $post['leader'] = $agentid;
                $result=$this->agentapi_service->addAgent($post);
                echo $result;
                break;
            case 'PUT':
                /**
                 * 代理修改资料
                 * @param tooken 代理临时通行证
                 * @param agent_name    名字
                 * @param mobile        手机号
                 * @param sex           性别
                 */
                $tooken = $this->input->input_stream('tooken');
                if(empty($tooken)){
                    $data['success'] = error('4008');
                    echo _echo($data);
                    exit;
                }
                $agentid=jiemi($tooken);
                $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));

                if(empty($agent)){
                    $data['success'] = error('4011');
                    echo _echo($data);
                    exit;
                }
                $agent_name=$this->input->input_stream('agent_name');
                if($agent_name){
                    $put['agent_name'] =$agent_name;
                }
                $mobile= $this->input->input_stream('mobile');
                if($mobile){
                    $put['mobile'] =$mobile;
                }
                $sex = $this->input->input_stream('sex');
                if($sex){
                    $put['sex'] =$sex;
                }
                // $headimg = $this->input->input_stream('headimg');
                // if($headimg){
                //     $put['headimg'] =$headimg;
                // }
                $put['agent_qq']=$agent['agent_qq'];
                $result=$this->agentapi_service->updateAgentByID(array('id'=>$agent['id']),$put);
                echo $result;
                break;
            case 'DELETE':
                
                break;

            default:
                # code...
                break;
    	}
    }


    /**
     * 代理管理中心接口
     * @param tooken 代理临时通行证
     */
    function agentinfo(){
        $tooken = $this->input->get('tooken');
        if(empty($tooken)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agentid=jiemi($tooken);
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        //总收入
        $moneywhere['agent_user_id']=$agent['id'];
        $money=$this->Commission_m->getAllMoney($moneywhere);
        if(empty($money)){
            $money=0;
        }
        //设备
        if($agent['agent_level']=='1'){
            $deviccewhere['agent1']=$agent['id'];
            $agentinfo['agent2count']=$this->agentapi_service->getloweragentnum(array('leader'=>$agent['id']));
        }elseif($agent['agent_level']=='2'){
            $deviccewhere['agent2']=$agent['id'];
        }
        $device=$this->Device_m->getDevicecount($deviccewhere);
        //充值记录 查这个代理的设备 再查这些设备的充值记录
        $deviccewhere['jihuo']=1;
        $deviceid=$this->Device_m->getDeviceid($deviccewhere);//设备
        $order=0;
        if($deviceid){            
            foreach ($deviceid as $key => $value) {
                $order+=$this->Order_m->getOrderCount(array('paystate'=>1,'device_id'=>$value['device_sn']));
            }
        }
        $agentinfo['success'] = success('4000');
        $agentinfo['agent']=$agent;
        $agentinfo['money']=$money;
        $agentinfo['device']=$device;
        $agentinfo['order']=$order;
        echo _echo($agentinfo);
        exit;
    }


    /**
     * 一级代理获取二级代理列表
     * @param tooken        代理临时通行证
     */
    function agent2List(){
        $tooken = $this->input->get('tooken');
        if(empty($tooken)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agentid=jiemi($tooken);
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        $result=$this->agentapi_service->getloweragent(array('leader'=>$agent['id']));
        echo $result;
        exit;
    }


    /**
     * 代理所属设备充值 代理每个月结算记录
     * @param tooken        代理临时通行证
     * @param page          页码
     * @param limit         每页条数
     */
    function monthMoneyList(){
        $tooken = $this->input->get('tooken');
        if(empty($tooken)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agentid=jiemi($tooken);
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        $where['agent_id'] = $agent['id'];
        $page = $this->input->get('page');
        if(empty($page)){
            $page=1;
        }
        $limit = $this->input->get('limit');
        if(empty($limit)){
            $limit=5;
        }
        $offset=($page-1)*$limit;
        $orderby=' id desc ';
        $monthMoneyList=$this->Settlementlog_m->select($where,$orderby,$limit,$offset);
        $data['success'] = success('4000');
        $data['monthMoneyList']=$monthMoneyList;
        $data['money']=$this->Commission_m->getmost(array('agent_user_id'=>$agent['id']));
        echo _echo($data);
        exit;

    }

    /**
     * 代理所属设备充值 代理的收入记录
     * @param tooken        代理临时通行证
     * @param month         月份
     * @param page          页码
     * @param limit         每页条数
     */
    function commissionList(){
        $tooken = $this->input->get('tooken');
        $month = $this->input->get('month');        
        if(empty($tooken)||empty($month)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agentid=jiemi($tooken);
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        $where['agent_user_id'] = $agent['id'];
        $where['settlement'] = 1;

        $timestamp = strtotime($month.'01');
        $start_time = strtotime(date('Y-m-1 00:00:00',$timestamp));
        $mdays = date('t',$timestamp);
        $end_time = strtotime(date('Y-m-'.$mdays.' 23:59:59',$timestamp));
        $where['addtime2 >= '] = $start_time;
        $where['addtime2 <= '] = $end_time;
        $page = $this->input->get('page');
        if(empty($page)){
            $page=1;
        }
        $limit = $this->input->get('limit');
        if(empty($limit)){
            $limit=5;
        }
        $commissionList=$this->Commission_m->getList($where,$page,$limit);
        $data['success'] = success('4000');
        $data['commissionList']=$commissionList;
        $data['money']=$this->Commission_m->getmost($where);
        echo _echo($data);
        exit;
    }

    /**
     * 代理所属设备充值 代理未结算的收入记录
     * @param tooken        代理临时通行证
     * @param page          页码
     * @param limit         每页条数
     */
    function noSettlementList(){
        $tooken = $this->input->get('tooken');     
        if(empty($tooken)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agentid=jiemi($tooken);
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        $where['agent_user_id'] = $agent['id'];
        $where['settlement'] = 0;

        $page = $this->input->get('page');
        if(empty($page)){
            $page=1;
        }
        $limit = $this->input->get('limit');
        if(empty($limit)){
            $limit=5;
        }
        $commissionList=$this->Commission_m->getList($where,$page,$limit);
        $data['success'] = success('4000');
        $data['commissionList']=$commissionList;
        $data['money']=$this->Commission_m->getmost($where);
        echo _echo($data);
        exit;
    }


    /**
     * 代理所属设备充值 代理的收入记录
     * @param agentid       代理ID
     * @param page          页码
     * @param limit         每页条数
     */
    function getCommissionListByAgentid(){
        $agentid=$this->input->get('agentid');//代理id
        if(empty($agentid)){
            $data['success'] = error('4008');               
            echo _echo($data);
            exit;
        }
        $agent=$this->Agent_m->select(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }else{
            $agent=$agent[0];
        }
        $where['agent_user_id'] = $agent['id'];
        $page = $this->input->get('page');
        if(empty($page)){
            $page=1;
        }
        $limit = $this->input->get('limit');
        if(empty($limit)){
            $limit=5;
        }
        $commissionList=$this->Commission_m->getList($where,$page,$limit);
        $data['success'] = success('4000');
        $data['commissionList']=$commissionList;
        $data['money']=$this->Commission_m->getmost($where);
        echo _echo($data);
        exit;
    }


    /**
     * 代理所属设备的充值记录
     * @param tooken        代理临时通行证
     * @param page          页码
     * @param limit         每页条数
     */
    function OrderList(){
        $tooken = $this->input->get('tooken');
        if(empty($tooken)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agentid=jiemi($tooken);
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        $page = $this->input->get('page');
        if(empty($page)){
            $page=1;
        }
        $limit = $this->input->get('limit');
        if(empty($limit)){
            $limit=5;
        }
        //设备
        if($agent['agent_level']=='1'){
            $deviccewhere['agent1']=$agent['id'];
        }elseif($agent['agent_level']=='2'){
            $deviccewhere['agent2']=$agent['id'];
        }
        $taocanlist=$this->Model_m->select('recharge');
        if($taocanlist){
            foreach ($taocanlist as $key => $value) {
                $taocan[$value['package_id']]=$value;
            }
        }
        //充值记录 查这个代理的设备 再查这些设备的充值记录
        $deviccewhere['jihuo']=1;
        $deviceid=$this->Device_m->getDeviceid($deviccewhere);//设备
        if($deviceid){            
            foreach ($deviceid as $key => $value) {
                $deviceids[]=$value['device_sn'];
            }
            $OrderList=$this->Order_m->getOrderList($deviceids,$page,$limit);
            // foreach ($OrderList as $key => $value) {
            //     $OrderList[$key]['price']=$taocan[$value['goods_on']]['price'];
            //     $OrderList[$key]['flow']=$taocan[$value['goods_on']]['flow'];
            // }
        }else{
            $OrderList=array();
        }
        $data['success'] = success('4000');
        $data['OrderList']=$OrderList;
        $data['taocan']=$taocan;
        echo _echo($data);
        exit;
    }


    //获取微信签名用于前端禁止分享
    public function getWeixinSign(){
        $where['inc_type']='weixin';
        $weixin  = $this->Config_m->getconfig($where);
        $JssdkLogic = new JssdkLogic($weixin['appid'],$weixin['secret']);
        $data = $JssdkLogic->getSignPackage();
        $Sign['success'] = success('4000');  
        $Sign['Sign'] = $data;
        echo _echo($Sign);
        exit;
    }


    /**
     * 获取下级代理列表
     * @param leader 上级代理id
     */
    public function getloweragent(){
        $this->islogin();
        $where['leader'] = $this->input->get('leader');
        if($where['leader']){
            $result=$this->agentapi_service->getloweragent($where,$put);
            echo $result;
        }else{
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
    }


    /**
     * 通过QQ号获取代理信息
     * @param agent_qq 代理的qq账号
     */
    function getAgentByqq(){
        $where['agent_qq'] = $this->input->get('agent_qq');
        if($where['agent_qq']){
            $result=$this->Agent_m->select($where);
            $data['success'] = success('4000');
            if($result){
                $data['agentinfo']=$result[0];
            }
            echo _echo($data);
            exit;
        }else{
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
    }


    /**
     * 查看别人充值记录
     * @param agentid        代理ID
     * @param page          页码
     * @param limit         每页条数
     */
    function OrderListByAgentid(){
        $agentid = $this->input->get('agentid');
        if(empty($agentid)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $agent=$this->agentapi_service->getAgentById(array('id'=>$agentid));
        if(empty($agent)){
            $data['success'] = error('4011');
            echo _echo($data);
            exit;
        }
        $page = $this->input->get('page');
        if(empty($page)){
            $page=1;
        }
        $limit = $this->input->get('limit');
        if(empty($limit)){
            $limit=5;
        }
        //设备
        if($agent['agent_level']=='1'){
            $deviccewhere['agent1']=$agent['id'];
        }elseif($agent['agent_level']=='2'){
            $deviccewhere['agent2']=$agent['id'];
        }
        //充值记录 查这个代理的设备 再查这些设备的充值记录
        $deviccewhere['jihuo']=1;
        $deviceid=$this->Device_m->getDeviceid($deviccewhere);//设备
        if($deviceid){            
            foreach ($deviceid as $key => $value) {
                $deviceids[]=$value['device_sn'];
            }
            $OrderList=$this->Order_m->getOrderList($deviceids,$page,$limit);
        }else{
            $OrderList=null;
        }
        $taocanlist=$this->Model_m->select('recharge');
        if($taocanlist){
            foreach ($taocanlist as $key => $value) {
                $taocan[$value['package_id']]=$value;
            }
        }
        $data['success'] = success('4000');
        $data['OrderList']=$OrderList;
        $data['taocan']=$taocan;
        $data['agent']=$agent;
        echo _echo($data);
        exit;
    }

    public function sendmsg(){
        $where['inc_type']='weixin';
        $weixin  = $this->Config_m->getconfig($where);
        $JssdkLogic = new JssdkLogic($weixin['appid'],$weixin['secret']);
        $data = $JssdkLogic->push_msg('o2pEA1tVIaEg8IrqQYf8F7Y4syFw','414 414 414');
    }







    //判断是否带正确的登录参数
    function islogin(){
        $tooken = $this->input->get('tooken');
        //没接收到参数
        if(empty($tooken)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }else{
            //解密解不出来
            $adminid=jiemi($tooken);
            if(empty($adminid)){
                $data['success'] = error('4008');
                echo _echo($data);
                exit;
            }
            //没有查到该管理员
            $admin=$this->Admin_m->select(array('admin_id'=>$adminid));
            if(empty($admin)){
                $data['success'] = error('4008');
                echo _echo($data);
                exit;
            }
            return $admin;
        }
    }

    /**
     * 获取一级代理列表 下拉列表
     * @param tooken    登录通行证
     */
    public function getAllAgent1(){
        $this->islogin();
        $where['agent_level']=1;
        $data=$this->Agent_m->select($where);
        $data['success'] = success('4000');
        echo _echo($data);
        exit;
    }


    /**
     * 获取某个一级代理的二级代理列表 下拉列表
     * @param tooken    登录通行证
     */
    public function getAllAgent2ByLeader(){
        $this->islogin();
        $leader = $this->input->post('leader');
        if(empty($leader)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $where['leader']=$leader;
        $where['agent_level']=2;
        $data=$this->Agent_m->select($where);
        $data['success'] = success('4000');
        echo _echo($data);
        exit;
    }


    /**
     * 后台添加一级代理
     * @param tooken        登录通行证
     * @param agent_qq      账号
     * @param agent_name    名字
     * @param sex           性别
     * @param mobile        手机号
     * @param idcard        身份证号
     * @param bankcard      银行卡号
     * @param bankname      银行名称
     */
    public function addagent1(){
        $admin=$this->islogin();
        $agent_qq = $this->input->post('agent_qq');
        if($agent_qq){
            $post['agent_qq'] =$agent_qq;
        }
        $agent_name = $this->input->post('agent_name');
        if($agent_name){
            $post['agent_name'] =$agent_name;
        }
        $mobile = $this->input->post('mobile');
        if($mobile){
            $post['mobile'] =$mobile;
        }
        $idcard = $this->input->post('idcard');
        if($idcard){
            $post['idcard'] =$idcard;
        }
        $sex = $this->input->post('sex');
        if($sex){
            $post['sex'] =$sex;
        }
        $bankcard = $this->input->post('bankcard');
        if($bankcard){
            $post['bankcard'] =$bankcard;
        }
        $bankname = $this->input->post('bankname');
        if($bankname){
            $post['bankname'] =$bankname;
        }
        $branch = $this->input->post('branch');
        if($branch){
            $post['branch'] =$branch;
        }
        if(empty($post['agent_qq'])||empty($post['agent_name'])||empty($post['mobile'])||empty($post['sex'])||empty($post['bankname'])||empty($post['bankcard'])||$post['idcard']){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $post['addtime'] = date('YmdHis');
        $post['agent_level'] = 1;
        $post['leader'] = 0;
        $result=$this->agentapi_service->addAgent($post);
        $info=json_decode($result);
        if($info['success']['code']=='4000'){
            //添加一级代理日志
            $log['admin_id']=$admin[0]['admin_id'];
            $log['log_info']='添加一级代理'.json_encode($post);
            $log['log_ip']=$_SERVER["REMOTE_ADDR"];
            $log['log_time']=time();
            $this->Adminlog_m->insert($log);
        }
        echo $result;
        exit;
    }


    /**
     * 查看某个代理资料
     * @param tooken        登录通行证
     * @param id            代理id
     */
    public function getAgentinfoById(){
        $this->islogin();
        $where['id'] = $this->input->get('id');
        if($where['id']){
            $result=$this->Agent_m->select($where);
            $data['success'] = success('4000');
            if($result){
                $data['agentinfo']=$result[0];
            }
            echo _echo($data);
            exit;
        }else{
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
    }


    /**
     * 修改某个代理资料
     * @param tooken        登录通行证
     * @param id            代理id
     * @param mobile        手机号
     * @param idcard        身份证号码
     * @param bankname      银行名称
     * @param branch        支行信息
     * @param bankcard      银行卡号
     */
    public function updateAgentinfoById(){
        $admin=$this->islogin();
        $id=$this->input->post('id');
        $data=$this->input->post();
        if(empty($id)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $where['id'] = $this->input->get('id');
        if($where['id']){
            $result=$this->Agent_m->select($where);
            $data['success'] = success('4000');
            if($result){
                $data['agentinfo']=$result[0];
            }
            echo _echo($data);
            exit;
        }else{
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }

        if($info['success']['code']=='4000'){
            //添加一级代理日志
            $log['admin_id']=$admin[0]['admin_id'];
            $log['log_info']='修改代理资料'.json_encode($post);
            $log['log_ip']=$_SERVER["REMOTE_ADDR"];
            $log['log_time']=time();
            $this->Adminlog_m->insert($log);
        }

    }


    /**
     * 总后台获取一级代理列表
     * @param tooken        登录通行证
     * @param mobile        手机号
     * @param page          页码
     * @param limit         每页条数
     */
    public function getAgent1List(){
        $this->islogin();
        $mobile = $this->input->get('mobile');
        $page=$this->input->get('page');
        $limit=$this->input->get('limit');
        if(empty($page)||empty($limit)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        if($mobile){
            $where['mobile']=$mobile;
        }
        $where['agent_level']=1;
        $data=$this->agentapi_service->getAgentList($where,$page,$limit);
        $data['success'] = success('4000');
        echo _echo($data);
        exit;
    }
    

    /**
     * 总后台获取二级代理列表
     * @param tooken        登录通行证
     * @param mobile        手机号
     * @param page          页码
     * @param limit         每页条数
     */
    public function getAgent2List(){
        $this->islogin();
        $mobile = $this->input->get('mobile');
        $page=$this->input->get('page');
        $limit=$this->input->get('limit');
        if(empty($page)||empty($limit)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        if($mobile){
            $where['mobile']=$mobile;
        }
        $where['agent_level']=2;
        $data=$this->agentapi_service->getAgentList($where,$page,$limit);
        $data['success'] = success('4000');
        echo _echo($data);
        exit;
    }


    /**
     * 通过代理名称搜索代理
     * @param tooken        登录通行证
     * @param name          代理名称
     */
    public function getAgentByName(){
        $this->islogin();
        $where['agent_name'] = $this->input->get('name');
        if($where['agent_name']){
            $result=$this->Agent_m->select($where);
            $data['success'] = success('4000');
            if($result){
                $data['agentinfo']=$result[0];
            }
            echo _echo($data);
            exit;
        }else{
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
    }


    //获取套餐信息
    function getRecharge(){
        $data=$this->Recharge_m->select();
        foreach ($data as $key => $value) {
            $recharge[$value['package_id']]=$value;
        }
        return $recharge;
    }

    //获取分佣比例信息
    function getProportion(){
        $data=$this->Model_m->select('mifi_config',array('inc_type'=>'agent'));
        foreach ($data as $key => $value) {
            if($value['name']=='agent1'){
                $return[1]=$value['value'];
            }
            if($value['name']=='agent2'){
                $return[2]=$value['value'];
            }
        }
        return $return;
    }


    /**
     * 总后台获取订单
     * @param tooken        登录通行证
     * @param device_sn     设备编号
     * @param start_time    开始时间
     * @param end_time      结束时间
     * @param page          页码
     * @param limit         每页条数
     */
    public function getOrderList(){
        $this->islogin();
        $device_sn = $this->input->get('device_sn');
        $start_time = $this->input->get('start_time');
        $end_time = $this->input->get('end_time');
        $page=$this->input->get('page');
        $limit=$this->input->get('limit');
        //$page=1;
        //$limit=10;
        //$start_time="2018-03-02 06:24:00";
        //$end_time="2018-03-02 23:24:00";
        if(empty($page)||empty($limit)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $where['paystate']=1;
        if($device_sn){
            $where['device_sn']=$device_sn;
        }
        if($start_time){
            $where['endtime > ']=$start_time;
        }
        if($end_time){
            $where['endtime < ']=$end_time;
        }
        $offset=($page-1)*$limit;
        $orderby=' orderid desc ';
        $result=$this->Order_m->select($where,$orderby,$limit,$offset);        
        if($result){
            $recharge=$this->getRecharge();
            foreach ($result as $key => $value) {
                $result[$key]['flow']=$recharge[$value['goods_on']]['flow'];
                $device=$this->Device_m->select(array('device_sn'=>$value['device_id']));
                if($device[0]['agent2']){
                    $agentid=$device[0]['agent2'];
                }elseif($device[0]['agent1']){
                    $agentid=$device[0]['agent1'];
                }
                $agent=$this->Agent_m->select(array('id'=>$agentid));
                $result[$key]['agent_name']=$agent[0]['agent_name'];
            }
        }
        $data['success'] = success('4000');
        if($result){
            $data['orderList']=$result;
        }
        echo _echo($data);
        exit;
    }


    /**
     * 总后台获取佣金记录
     * @param tooken        登录通行证
     * @param device_sn     设备编号
     * @param start_time    开始时间
     * @param end_time      结束时间
     * @param page          页码
     * @param limit         每页条数
     */
    public function getCommissionList(){
        $this->islogin();
        $device_sn = $this->input->get('device_sn');
        $start_time = $this->input->get('start_time');
        $end_time = $this->input->get('end_time');
        $page=$this->input->get('page');
        $limit=$this->input->get('limit');
        // $page=1;
        // $limit=10;
        // $start_time="2018-03-02 06:24:00";
        // $end_time="2018-03-02 23:24:00";
        if(empty($page)||empty($limit)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        if($device_sn){
            $where['device_sn']=$device_sn;
        }
        if($start_time){
            $where['addtime > ']=$start_time;
        }
        if($end_time){
            $where['addtime < ']=$end_time;
        }
        $offset=($page-1)*$limit;
        $orderby=' id desc ';
        $result=$this->Commission_m->select($where,$orderby,$limit,$offset);
        if($result){
            $proportion=$this->getProportion();
            foreach ($result as $key => $value) {
                $agent=$this->Agent_m->select(array('id'=>$value['agent_user_id']));
                $result[$key]['agent_name']=$agent[0]['agent_name'];
                $proportion=$proportion[$agent[0]['agent_level']]*100;
                $result[$key]['proportion']=$proportion.'%';
                $order=$this->Order_m->select(array('order_on'=>$value['order_id']));
                $result[$key]['order_money']=$order[0]['order_money'];
            }
        }
        $data['success'] = success('4000');
        if($result){
            $data['orderList']=$result;
        }
        echo _echo($data);
        exit;
    }


    /**
     * 会员列表
     * @param tooken        登录通行证
     * @param mobile        手机号
     * @param page          页码
     * @param limit         每页条数
     */
    public function getUserList(){
        $this->islogin();
        $mobile = $this->input->get('mobile');
        $page=$this->input->get('page');
        $limit=$this->input->get('limit');
        // $page=1;
        // $limit=10;
        // $mobile='15002060103';
        if(empty($page)||empty($limit)){
            $data['success'] = error('4008');
            echo _echo($data);
            exit;
        }
        $where=array();
        if($mobile){
            $where['mobile']=$mobile;
        }

        $offset=($page-1)*$limit;
        $orderby=' user_id desc ';
        $result=$this->Users_m->select($where,$orderby,$limit,$offset);
        $data['success'] = success('4000');
        if($result){
            $data['userList']=$result;
        }
        echo _echo($data);
        exit;
    }













}

